<?php

namespace App\Dao;

use App\Model\UsersStockModel;

class UserStockDao extends BaseDao
{
    public static function get_user_stock_builder($params = []){
        $query = UsersStockModel::query();

        $query->when(checkIsHasField($params,'stock_id'),function ($query)use ($params){
            $query->where('stock_id',$params['stock_id']);
        });


        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });

        $query->when(checkIsHasField($params,'project_id'),function ($query)use ($params){
            $query->where('project_id',$params['project_id']);
        });

        $query->when(checkIsHasField($params,'category_id'),function ($query)use ($params){
            $query->where('category_id',$params['category_id']);
        });





        return $query;
    }

    public static function get_user_stock(array $array)
    {
        return CategoryDao::get_category_builder([

        ])->with(['userStock'=>function($query)use($array){
            $query->where('user_id',$array['user_id']);
        },'userStock.goods'])->whereHas('userStock')->get();
    }



    public static function all_stock($params = []){
        return self::get_user_stock_builder([
            'user_id'=>$params['user_id']
        ])->with('goods')->get();
    }

    public static function get_user_stock_page(array $all)
    {
        return self::get_user_stock_builder($all)->with(['userInfo','goods'])->paginate();
    }
}
